﻿using Common.Regexs;
using Common.Utils;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using RepositorySys.Filte;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using utils;
using utils.enums;
using utils.EX;
using Utils;

namespace RepositorySys.Controllers
{
    [CustomActionFilte]
    public class DepartmentController : Controller
    {
        //部门接口
        IDepartmentBLL _departmentBLL;

        //用户接口
        IUserBLL _userBLL;
        public DepartmentController(IDepartmentBLL departmentBLL, IUserBLL userBLL)
        {
            _departmentBLL = departmentBLL;
            _userBLL = userBLL;
        }

        #region 视图

        /// <summary>
        /// 部门列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddView()
        {
            return View();
        }

        /// <summary>
        /// 修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdataView()
        {
            return View();
        }
        #endregion

        /// <summary>
        /// 部门列表、搜索
        /// </summary>
        /// <param name="DepartmentName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getDepartmentList(string DepartmentName, string page, string limit)
        {
            List<DepartmentDTO> department = null;
            int cont = 0;
            try
            {
                department = _departmentBLL.getdepartmentList(DepartmentName, page, limit, out cont);
            }
            catch (MyException e)
            {
                //获取异常信息
                return getException(department, e);
            }
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), cont, department));
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="department">部门信息</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult AddDepartment(DepartmentInfo department)
        {
            //状态信息
            string mgs; 
            object data = null;
            if (Verify(department, out mgs))
            {
                try
                {
                    data = _departmentBLL.AddDepartment(department);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            return Json(new JsonResults(200, string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.SUCCESS) : mgs, data));
        }


        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateDepartment(string departmentId)
        {
            object data = null;
            try
            {
                data = _departmentBLL.DalateDepartment(departmentId);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="departmentIds"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateDepartmentList(List<string> departmentIds)
        {
            object data = null;
            try
            {
                data = _departmentBLL.DalateDepartmentList(departmentIds);
            }
            catch (MyException e)
            {
                 return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }

        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        public IActionResult UpdataDepartment(DepartmentInfo department)
        {
            object data = null;
            string mgs = null;
            if (Verify(department,out mgs))
            {
                try
                {
                    data = _departmentBLL.UpdataDepartment(department);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            string a = string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.UPDATE) : mgs;
            return Json(new JsonResults(200,a, data));
        }

        /// <summary>
        /// 根据部门id搜索(修改页面数据列表)
        /// </summary>
        /// <param name="departmentid">部门id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getByDepartment(string departmentid)
        {
            //部门数据集
            DepartmentInfo department = _departmentBLL.getByDepartment(departmentid);

            //获取主管数据集
            object leader = _userBLL.getLeader(departmentid);
            var datas = new
            {
                department,
                leader
            };
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), datas));
        }

        /// <summary>
        /// 获取全部部门信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getDepartment( )
        {
            List<DepartmentDTO> data = _departmentBLL.getDepartment();
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), data));
        }

        /// <summary>
        /// 部门树形下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getDepartmentSelect()
        {
            return Json(_departmentBLL.getDepartmentSelect());
        }


        /// <summary>
        /// 主管下拉 框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getLeader()
        {
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), _userBLL.getLeader(null)));
        }

        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="department">参数</param>
        /// <returns></returns>
        private bool Verify(DepartmentInfo department, out string mgs)
        {
            Regex re = new Regex(Regular.Name);
            if (!re.Match(department.DepartmentName).Success)
            {
                mgs = "请输入正确的部门名称";
                return false;
            }
            else if (department.LeaderId.EndsWith("0"))
            {
                mgs = "请选择主管";
                return false;
            }
            else if (department.ParentId != null ? string.IsNullOrEmpty(department.ParentId) : false)
            {
                mgs = "请选择父部门";
                return false;
            }
            else if (department.Description != null ? !new Regex(Regular.Description).Match(department.Description).Success : true)
            {
                mgs = "请输入 2-36 内的字符的描述信息";
                return false;
            }
            mgs = null;
            return true;
        }

        /// <summary>
        /// 获取异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private IActionResult getException(object data, MyException e)
        {
            var tt = e.GetExceptionResult();
            return Json(new JsonResults(tt.resultStatus, tt.resultMsg, data));
        }
    }
}
